<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<% 
String idEventu=request.getParameter("event_id");
int event_id = Integer.parseInt(idEventu);
%>
    <%@ page import="servlets.*"%>
<%@ page import="org.hibernate.SessionFactory"%>
<%@ page import="org.hibernate.Session"%>
<%@ page import="java.util.List"%>
<%@ page import="hibernate.model.*"%>
<%@ page import="hibernate.util.HibernateUtil"%>
<%@ page import="hibernate.method.*"%>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<%
						SessionFactory sessionFactory = HibernateUtil
								.getSessionAnnotationFactory();
						Session s = sessionFactory.openSession();
						s.beginTransaction();
						UserMethods um= new UserMethods();
						EventMethods em = new EventMethods();
						LocalMethods lm = new LocalMethods();
						FeedbackMethods fb = new FeedbackMethods();
						List<EventFeedback> listfb = fb.listEventFeedbacks(sessionFactory, s, event_id);
							response.setContentType("text/html");
							UserEvent e = em.findUserEventById(sessionFactory, s, event_id).get(0);
							Integer user = (Integer) session.getAttribute("user_id");
					%> 
				
<div class="page-header">
	<h2 class="page-title">Wydarzenia  -  <%=e.getEventname() %></h2>
</div>
<div id="content">
<br />
<% if (user != null){ %>
			<table>

<tr>
<td>
		<div class="col-md-4 col-sm-6" style="width: 95%;">
											<div class="team-member">
												 <div class="member-thumb">
												<!--	<img src="images/team3.jpg" alt=""> -->
												</div>
												<div class="member-infos">
													<h4 class="member-name">
													<%out.println("<a class=\"events\" id=\""+ event_id + "\" href=\"#\">" + e.getEventname() + "</a>"); %></h4>
													<span class="member-role">
													<%
													int lokal_id = e.getLocal_id();
													Local l = lm.findLocalById(sessionFactory, s, lokal_id);
													String localName = l.getCountry()+", "+l.getCity()+", "+ l.getStreet()+" "+l.getLocal_number();
													
													out.println("Lokal: "+ localName); %>
													<br />
													<%//out.println("Komentarzy: "+ commentsCount); %>
													</span>
													
													<% String opis1 = null;
													if (e.getDescription()== null || e.getDescription().equals("0")) opis1 = "Brak";
													else opis1 = e.getDescription();%>
													<p class="member-desc"><%out.println("Opis wydarzenia: "+ opis1);%></p>
													<br>
													<%// if(em.findByUserEvent(sessionFactory, s, user, event_id)==null ||em.findByUserEvent(sessionFactory, s, user, event_id).isEmpty() || em.findByUserEvent(sessionFactory, s, user, event_id).get(0).getMaybe()== true){ %>
													<%if (em.findByUserEvent(sessionFactory, s, user, event_id).size()<1 || em.findByUserEvent(sessionFactory, s, user, event_id).get(0).getMaybe() == true){ %>
													<form role="form" id='addEventParticipation' action="../addEventParticipation" method="POST">
													<input type="hidden" id="user_id"  name ="user_id" value="<%=user%>">
													<input type="hidden" id="event_id" name="event_id" value="<%=e.getUserevent_id()%>">
													<input type="hidden" id="type" name="type" value="false" >
														<input type="submit" class="mainBtn" value="Przyjdę!" />	
													</form>
													<%}	%>
													<br>
													<%// if(em.findByUserEvent(sessionFactory, s, user, event_id)==null ||em.findByUserEvent(sessionFactory, s, user, event_id).isEmpty() || em.findByUserEvent(sessionFactory, s, user, event_id).get(0).getMaybe()== false){ %>
													<%if (em.findByUserEvent(sessionFactory, s, user, event_id).size()<1 || em.findByUserEvent(sessionFactory, s, user, event_id).get(0).getMaybe() == false) {%>
													<form role="form" id='addEventParticipation' action="../addEventParticipation" method="POST">
													<input type="hidden" id="user_id" name ="user_id" value="<%=user%>">
													<input type="hidden" id="event_id" name="event_id" value="<%=e.getUserevent_id()%>">
													<input type="hidden" id="type" name="type" value="true" >
														<input type="submit" class="mainBtn" value="Zastanawiam się" />	
													</form>
													<%}	%>
													
													<% if(em.findByUserEvent(sessionFactory, s, user, event_id).size()>0){ %>
													<form role="form" id='deleteEventParticipation' action="../deleteEventParticipation" method="POST">
													<input type="hidden" id="user_id" name ="user_id" value="<%=user%>">
													<input type="hidden" id="event_id" name="event_id" value="<%=e.getUserevent_id()%>">
														<input type="submit" class="mainBtn" value="Nie przyjdę" />	
													</form>
													<%}	%>
													<div class="page-header">
													<% int participiansCount = em.CountAllPatricipians(sessionFactory, s, event_id); %>
	<h2 class="page-title" style="color: black;">Uczestnicy (<%=participiansCount%>)</h2>
</div>
<% List<User> epp = em.listUsersParticipatingForSure(sessionFactory, s, event_id);
out.println("Przyjdą na pewno: ");
for (User u: epp){
	out.println(u.getUsername());
}

List<User> eppm = em.listUsersParticipatingMaybe(sessionFactory, s, event_id);
out.println("Zastanawiają się: ");
for (User u: eppm){
	out.println(u.getUsername());
}
%>

<div class="page-header">
	<h2 class="page-title" style="color: black;">Konwersacja</h2>
</div>
<form role="form" id='eventFeedback' action="../addEventFeedback" method="POST">
		<p class="member-desc" style="color: white;">
		<input type="hidden" name="event_id" id="event_id" value="<%=event_id%>" />
		<input type="hidden" id="user" name="user" value="<%=user%>" />
		
			<br>											
		<textarea name="comment" rows="3" class="form-control" id="comment"  placeholder="Tresc posta"></textarea>
				<input type="submit" class="mainBtn" value="Dodaj post" />	
			</p>
		</form>
		<p class="member-desc" style="color:white;">
													<%
													if (listfb.size()> 0){
														for (EventFeedback fb1 : listfb)
														{%>	
															<%String userName = null;
															if(!fb1.getUser_id().equals("null")){
															String user_id = fb1.getUser_id();
															User u = um.id_to_user(sessionFactory, s, Integer.parseInt(user_id));
															 userName = u.getUsername();
															} else userName = "Anonim";
															String comment = fb1.getComment();
															String add_date = fb1.getAdd_date();
															%>
														
														<form role="form" id='deleteFb'action="../deleteEventFeedback" method="POST">
														<input type="hidden" id="comment_id" name="comment_id" value="<%=fb1.getId()%>" />
<% if (user != null){
	if (um.id_to_user(sessionFactory, s, user).getUsername().equals(userName)){
		%><input type="submit" class="mainBtn" value="Usuń" />
<%
	}
}%>
</form>
														
															<table>
															<tr>
															<td>
															Uzytkownik:
															</td>
															<td><%
															out.println(userName);
															%>
															</td>
															</tr>
															<tr>
															<td>
															Data dodania:
															</td>
															<td>
															<%out.println(add_date); %>
															</td></tr>
															<tr><td>
															Tresc komentarza:
															</td>
															<td><%
															out.println(comment);
															%>
															</td>
															</tr>
															</table><%
															out.println("<br />");
														}
														
													}
													else if (listfb.size() == 0) out.println("Ten lokal nie ma jeszcze zadnych postow");
													%>
													</p>
												</div>
											</div>
												</div>
											</div>
										</div>
										
									
									</td>
									</tr>
									<tr> <td><br /></td></tr>
								
		
	
	</table>
	<%}
s.getTransaction().commit();	%>

</div>		
		